package m.jclecai.com.utils;

import android.util.Log;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

public class RandUtil {
	/**
	 * 得到随机数
	 *
	 * @param count
	 *            随机数个数
	 * @param max
	 *            随机数产生的最大数
	 * @param type
	 *            是否加 0
	 * @return
	 */
	public static ArrayList<String> getRandomNum7(int count, int max,
												  boolean type) {
		ArrayList<String> list = new ArrayList<String>();
		String str = "";
		while (list.size() < count) {
			int a = (int) (Math.random() * (max + 1));
			if (a == 0)
				continue;

			if (type) {
				if (a < 10)
					str = "0" + a;
				else
					str = "" + a;
			} else {
				str = "" + a;
			}

			if (list.contains(str)) {
				continue;
			}

			list.add(str);
		}
		return list;
	}

	/**
	 * 得到随机数
	 *
	 * @param count
	 *            随机数个数
	 * @param max
	 *            随机数产生的最大数
	 */
	public static ArrayList<String> getRandomNum(int count, int max) {
		ArrayList<String> list = new ArrayList<String>();
		String str = "";
		while (list.size() < count) {
			int a = (int) (Math.random() * (max + 1));
			str = "" + a;
			if (list.contains(str)) {
				continue;
			}

			list.add(str);
		}
		return list;
	}

	/**
	 * 得到随机数数组
	 *
	 * @param count
	 *            几注
	 * @param redNum
	 *            红球个数
	 * @param blueNum
	 *            蓝球个数
	 * @param max1
	 *            红球最大数
	 * @param max2
	 *            蓝球最大数
	 * @param type
	 *            是否加 0
	 * @return
	 */
	public static String[] getRandom(int count, int redNum, int blueNum,
									 int max1, int max2, boolean type, boolean canZero) {
		String[] str = new String[count];
		for (int i = 0; i < count; i++) {
			List<String> red = new ArrayList<String>();
			List<String> blue = new ArrayList<String>();
			while (red.size() < redNum) {
				String s = "";
				int a = (int) (Math.random() * (max1 + 1));
				if (!canZero) {
					if (a == 0)
						continue;
				}

				if (type) {
					if (a < 10)
						s = s + " 0" + a;
					else
						s = s + " " + a;
				} else {
					s = s + " " + a;
				}

				if (red.contains(s)) {
					continue;
				}

				red.add(s);
			}

			while (blue.size() < blueNum) {
				String s = "";
				int a = (int) (Math.random() * (max2 + 1));
				if (a == 0)
					continue;

				if (type) {
					if (a < 10)
						s = s + " 0" + a;
					else
						s = s + " " + a;
				} else {
					s = s + " " + a;
				}

				if (blue.contains(s)) {
					continue;
				}

				blue.add(s);
			}

			Collections.sort(red);
			Collections.sort(blue);
			if (blueNum != 0) {
				str[i] = red.toString().trim().replace("[", "")
						.replace("]", "").replace(",", "")
						+ "-"
						+ blue.toString().trim().replace("[", "")
						.replace("]", "").replace(",", "");
			} else {
				str[i] = red.toString().trim().replace("[", "")
						.replace("]", "").replace(",", "");
			}
		}

		return str;
	}

	/**
	 * 得到随机数数组
	 *
	 * @param count
	 *            几注
	 * @param redNum
	 *            红球个数
	 * @param blueNum
	 *            蓝球个数
	 * @param max1
	 *            红球最大数
	 * @param max2
	 *            蓝球最大数
	 * @param type
	 *            是否加 0
	 * @return
	 */
	public static List<List<String>> getRandom8(int count, int redNum, int blueNum,
									 int max1, int max2, boolean type, boolean canZero) {
		List<List<String>> all = new ArrayList<>();
		for (int i = 0; i < count; i++) {
			List<String> arrayList = new ArrayList<>();
			List<String> red = new ArrayList<String>();
			List<String> blue = new ArrayList<String>();
			while (red.size() < redNum) {
				String s = "";
				int a = (int) (Math.random() * (max1 + 1));
				if (!canZero) {
					if (a == 0)
						continue;
				}

				if (type) {
					if (a < 10)
						s = s + " 0" + a;
					else
						s = s + " " + a;
				} else {
					s = s + " " + a;
				}

				if (red.contains(s)) {
					continue;
				}

				red.add(s);
			}

			while (blue.size() < blueNum) {
				String s = "";
				int a = (int) (Math.random() * (max2 + 1));
				if (a == 0)
					continue;

				if (type) {
					if (a < 10)
						s = s + " 0" + a;
					else
						s = s + " " + a;
				} else {
					s = s + " " + a;
				}

				if (blue.contains(s)) {
					continue;
				}

				blue.add(s);
			}

			Collections.sort(red);
			Collections.sort(blue);
			arrayList.addAll(red);
			arrayList.addAll(blue);
			all.add(arrayList);
		}

		return all;
	}


	/**
	 * 得到随机数数组（加入星座）
	 *
	 * @param count
	 *            几注
	 * @param redNum
	 *            红球个数
	 * @param blueNum
	 *            蓝球个数
	 * @param max1
	 *            红球最大数
	 * @param max2
	 *            蓝球最大数
	 * @param type
	 *            是否加 0
	 * @param xzNum
	 * 			  星座关联的数字
	 * @return
	 */
	public static List<List<String>> getRandom8(int count, int redNum, int blueNum,
												int max1, int max2, boolean type, boolean canZero,String xzNum) {
		List<List<String>> all = new ArrayList<>();

		int maxNum = 0;
		try{
			xzNum = xzNum.replaceAll("\"","");
			String[] split = xzNum.split("-");
			String s1 = split[0].trim().split("[.]")[1];
			String s2 = split[1].trim().split("[.]")[1];
			maxNum = Integer.parseInt(s1);
			if(maxNum < Integer.parseInt(s2))
				maxNum = Integer.parseInt(s2);
		}catch (Exception e){
			e.printStackTrace();
		}
		int xzCount = (int) (Math.random() * (max1 + 1));
		for (int i = 0; i < count; i++) {
			List<String> arrayList = new ArrayList<>();
			List<String> red = new ArrayList<String>();
			List<String> blue = new ArrayList<String>();
			while (red.size() < redNum) {
				String s = "";
				int a = (int) (Math.random() * (max1 + 1));

				if (!canZero) {
					if (a == 0)
						continue;
				}
				//计算该范围内的数字
				if(maxNum < max1 && xzCount > 0){
					a = (int) (Math.random() * (maxNum + 1));
				}

				if (type) {
					if (a < 10)
						s = s + " 0" + a;
					else
						s = s + " " + a;
				} else {
					s = s + " " + a;
				}

				if (red.contains(s)) {
					continue;
				}

				red.add(s);
			}

			while (blue.size() < blueNum) {
				String s = "";
				int a = (int) (Math.random() * (max2 + 1));
				if (a == 0)
					continue;

				if (type) {
					if (a < 10)
						s = s + " 0" + a;
					else
						s = s + " " + a;
				} else {
					s = s + " " + a;
				}

				if (blue.contains(s)) {
					continue;
				}

				blue.add(s);
			}

			Collections.sort(red);
			Collections.sort(blue);
			arrayList.addAll(red);
			arrayList.addAll(blue);
			all.add(arrayList);
			xzCount --;
		}

		return all;
	}

	/**
	 * 得到随机数数组（加入生日固定号码）
	 *
	 * @param count
	 *            几注
	 * @param redNum
	 *            红球个数
	 * @param blueNum
	 *            蓝球个数
	 * @param max1
	 *            红球最大数
	 * @param max2
	 *            蓝球最大数
	 * @param type
	 *            是否加 0
	 * @param canZero
	 *            是否为 0
	 * @param data
	 * 			  生日数字
	 * @param birthIndex
	 * 			  生日数字下标用来设置背景色
	 *
	 * @return
	 */
	public static List<List<String>> getRandom8(int count, int redNum, int blueNum,
												int max1, int max2, boolean type, boolean canZero,String[] data, List<Map<Integer,Integer>> birthIndex) {
		List<List<String>> all = new ArrayList<>();
		for (int i = 0; i < count; i++) {
			List<String> arrayList = new ArrayList<>();
			List<String> red = new ArrayList<String>();
			List<String> blue = new ArrayList<String>();
			//生日号码个数
			int birthCount = 0;
			while (red.size() < redNum) {
				String s = "";
				int a = (int) (Math.random() * (max1 + 1));

				if(data != null && data.length > 0){
					//如果号码少于等于固定生日号码长度 则只取中间的一个号码
					if(redNum <= data.length && birthCount == 0){
						//三位数组合只使用月份作为固定生日数字(彩豆子 幸运选号规则)
						a = Integer.parseInt(data[1]);
					}else if(redNum > data.length && birthCount<data.length){
						a = Integer.parseInt(data[birthCount]);
					}
				}

				if (!canZero) {
					if (a == 0)
						continue;
				}

				if (type) {
					if (a < 10)
						s = s + " 0" + a;
					else
						s = s + " " + a;
				} else {
					s = s + " " + a;
				}

				if (red.contains(s)) {
					continue;
				}
				red.add(s);
				birthCount++;
			}
			Collections.shuffle(red);
			Map<Integer,Integer> map = new HashMap<>();
			//找到打乱后的生日下标
			for (int rd=0;rd<data.length;rd++){
				for (int r=0;r<red.size();r++){
					Log.e("num",red.get(r).trim()+"_"+((type?"0":"")+data[rd].trim()));
					if(redNum <= data.length){//如果号码少于等于固定生日号码长度 则只取中间的一个号码
						if(red.get(r).trim().equals((type?"0":"")+data[1].trim())){
							map.put(r,r);
							break;
						}
					}else{
						if(red.get(r).trim().equals((type?"0":"")+data[rd].trim())){
							map.put(r,r);
							break;
						}
					}
				}
			}
			birthIndex.add(map);
			while (blue.size() < blueNum) {
				String s = "";
				int a = (int) (Math.random() * (max2 + 1));
				if (a == 0)
					continue;

				if (type) {
					if (a < 10)
						s = s + " 0" + a;
					else
						s = s + " " + a;
				} else {
					s = s + " " + a;
				}

				if (blue.contains(s)) {
					continue;
				}

				blue.add(s);
			}

			Collections.sort(blue);
			arrayList.addAll(red);
			arrayList.addAll(blue);
			all.add(arrayList);
		}

		return all;
	}


	/**
	 * 得到随机数
	 *
	 * @param count
	 *            随机数个数
	 * @param max
	 *            随机数产生的最大数
	 * @return
	 */
	public static HashSet<String> getRandomNum2(int count, int max) {
		HashSet<String> list = new HashSet<String>();
		String str = "";
		while (list.size() < count) {
			int a = (int) (Math.random() * (max + 1));
			if (a == 0)
				continue;
			if (a < 10)
				str = "0" + a;
			else
				str = "" + a;
			if (list.contains(str)) {
				continue;
			}
			list.add(str);
		}
		return list;
	}

	/**
	 * 得到随机数
	 *
	 * @param count
	 *            随机数个数
	 * @param max
	 *            随机数产生的最大数(不包含)
	 * @return [0,max) 前面不加 0
	 */
	public static HashSet<String> getRandomNum3(int count, int max) {

		HashSet<String> list = new HashSet<String>();
		while (list.size() < count) {
			int a = (int) (Math.random() * (max));
			if (list.contains(a + "")) {

				continue;
			}
			list.add(a + "");

		}

		return list;
	}
}